var dictKeyArray = [];
layui.use(['element','table','form','layer','laydate'], function(){
    var table = layui.table,
        form = layui.form,
        $ = layui.jquery,
        laydate = layui.laydate,
        layer = layui.layer;

    // 有效时间
    renderTime('startDate','date');
    renderTime('endDate','date');
    // 时段
    renderTime('selection1Start','time');
    renderTime('selection1End','time');
    renderTime('selection2Start','time');
    renderTime('selection2End','time');
    renderTime('selection3Start','time');
    renderTime('selection3End','time');

    function renderTime(elemId,type){
        laydate.render({
            elem: '#'+elemId,
            trigger : 'click',
            type: type,
        });
    }

    addAuthTemplate = function () {
        $("#resource_form")[0].reset();
        $("#upload_form").attr("disabled", false);
        $("#upload_form").attr("class", "layui-btn");
        $("#sd1").attr("checked",false);
        $("#sd2").attr("checked",false);
        $("#sd3").attr("checked",false);
        for(var i=1;i<=7;i++){
            $("input[name='xq"+i+"']").attr("checked",false);
        }
        layer.open({
            type: 1
            , title: '添加权限模板'
            , area: ['55%', '90%']
            , content: $('#uploadDiv')
        });
    };

    table.render({
        elem: '#authTemplateTable'
        ,url:'/faceAcsAuthTemplate/list'
        ,title: '权限模板管理'
        ,where: {name:''}
        ,cols: [[
            {field: 'xh',type:'numbers', title: '序号',}
            ,{field:'id', title:'权限模板Id',align:'center'}
            ,{field:'name', title:'权限模板名称',align:'center'}
            ,{field:'updateUser', title:'修改人员',align:'center'}
            ,{field:'updateTime', title:'最后修改时间',  sort: true,templet:
                    function(data){
                        return formatTime(data.updateTime);
                    }}
            ,{field:'right',title:'操作',toolbar:'#barOperate',align:'center',width:220}
        ]]
        ,page: true
    });

    function formatTime(data){
        return data ? data.replace("T", " "):"";
    }

    table.on('tool(authTemplateTable)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'edit':
                $("#update_form").attr("disabled", false);
                $("#update_form").attr("class", "layui-btn");
                $("#resource_form")[0].reset();
                $("#id").val(data.id);
                $("#templateName").val(data.name)
                $("#startDate").val(data.startDate);
                $("#endDate").val(data.endDate);
                var s1s = data.selection1Start;
                var s1e = data.selection1End;
                if(!s1s && !s1e){
                    $("#sd1").attr("checked",false);
                    $("#selection1Start").attr("disabled", true);
                    $("#selection1End").attr("disabled", true);
                }else {
                    $("#sd1").attr("checked",true);
                    $("#selection1Start").removeAttr("disabled");
                    $("#selection1End").removeAttr("disabled");
                    $("#selection1Start").val(s1s);
                    $("#selection1End").val(s1e);
                }
                var s2s = data.selection2Start;
                var s2e = data.selection2End;
                if(!s2s && !s2e){
                    $("#sd2").attr("checked",false);
                    $("#selection2Start").attr("disabled", true);
                    $("#selection2End").attr("disabled", true);
                }else {
                    $("#sd2").attr("checked",true);
                    $("#selection2Start").removeAttr("disabled");
                    $("#selection2End").removeAttr("disabled");
                    $("#selection2Start").val(s2s);
                    $("#selection2End").val(s2e);
                }
                var s3s = data.selection3Start;
                var s3e = data.selection3End;
                if(!s3s && !s3e){
                    $("#sd3").attr("checked",false);
                    $("#selection3Start").attr("disabled", true);
                    $("#selection3End").attr("disabled", true);
                }else {
                    $("#sd3").attr("checked",true);
                    $("#selection3Start").removeAttr("disabled");
                    $("#selection3End").removeAttr("disabled");
                    $("#selection3Start").val(s3s);
                    $("#selection3End").val(s3e);
                }
                var weekStr = data.weekControl;
                if(weekStr){
                    var wArr = weekStr.split(",");
                    for (let i = 0; i < wArr.length ; i++) {
                        var checkedFlag = wArr[i] == 1 ? true : false;
                        $("input[name='xq"+(i+1)+"']").attr("checked",checkedFlag);
                    }
                }else {
                    for(var i=1;i<=7;i++){
                        $("input[name='xq"+i+"']").attr("checked",false);
                    }
                }
                form.render();
                layer.open({
                    type: 1
                    , title: '编辑权限模板'
                    , area: ['55%', '90%']
                    , content: $('#uploadDiv')
                });
                break;
            case 'delete':
                layer.confirm('确定要删除吗?', {
                }, function(){
                    layer.load();
                    $.get('/faceAcsAuthTemplate/delete',{id:data.id},function (res) {
                        layer.msg(res.msg,{
                            time:500,
                            end:function () {
                                table.reload('authTemplateTable');
                                layer.closeAll();
                            }
                        });
                    });
                });
                break;
        }
    });

    form.on('checkbox(sd1)', function(data){
        if(data.elem.checked){//是否被选中，true或者false
            $("#selection1Start").removeAttr("disabled");
            $("#selection1End").removeAttr("disabled");
        }else {
            $("#selection1Start").attr("disabled", true);
            $("#selection1End").attr("disabled", true);
        }
    });
    form.on('checkbox(sd2)', function(data){
        if(data.elem.checked){
            $("#selection2Start").removeAttr("disabled");
            $("#selection2End").removeAttr("disabled");
        }else {
            $("#selection2Start").attr("disabled", true);
            $("#selection2End").attr("disabled", true);
        }
    });
    form.on('checkbox(sd3)', function(data){
        if(data.elem.checked){
            $("#selection3Start").removeAttr("disabled");
            $("#selection3End").removeAttr("disabled");
        }else {
            $("#selection3Start").attr("disabled", true);
            $("#selection3End").attr("disabled", true);
        }
    });

    form.on('submit(query_device)',function () {
        var name = $("#name").val();
        table.reload('authTemplateTable',{
            where: {name:name}
            ,page:  {
                curr:1
            }
        });
        return false;
    });

    form.on('submit(upload_form)',function(data){
        $("#upload_form").attr("disabled", true);
        $("#upload_form").attr("class", "layui-btn layui-btn-disabled");
        if($("#sd1").prop('checked')){
            var s1 = $("#selection1Start").val();
            var e1 = $("#selection1End").val();
            if(!s1 || !e1){
                layer.msg("选择时段1开始时间和结束时间",{icon:5});
                return false;
            }
        }else {
            data.field.selection1Start = '';
            data.field.selection1End = '';
        }
        if($("#sd2").prop('checked')){
            var s2 = $("#selection2Start").val();
            var e2 = $("#selection2End").val();
            if(!s2 || !e2){
                layer.msg("选择时段2开始时间和结束时间",{icon:5});
                return false;
            }
        }else {
            data.field.selection2Start = '';
            data.field.selection2End = '';
        }
        if($("#sd3").prop('checked')){
            var s3 = $("#selection3Start").val();
            var e3 = $("#selection3End").val();
            if(!s3 || !e3){
                layer.msg("选择时段3开始时间和结束时间",{icon:5});
                return false;
            }
        }else {
            data.field.selection3Start = '';
            data.field.selection3End = '';
        }
        var weekControlStr = '';
        for(var i=1;i<=7;i++){
            var val = $("input[name='xq"+i+"']").prop('checked');
            weekControlStr += val ? '1,' : '0,';
        }
        data.field.weekControl = weekControlStr.substr(0,weekControlStr.lastIndexOf(","));
        layer.load();
        $.ajax({
            type: 'post',
            url: '/faceAcsAuthTemplate/saveOrUpdate',
            data: data.field,
            success: function (res) {
                layer.msg(res.msg);
                setTimeout(function (){
                    layer.closeAll();
                },500);
                $("#upload_form").attr("disabled", false);
                $("#upload_form").attr("class", "layui-btn");
                table.reload('authTemplateTable');
            },
        });
        return false;
    });

    $("#cancel_form").on('click',function (){
        layer.closeAll();
        return false;
    });

});

function parseStatus(status) {
    if(typeof status == "undefined" || status == null){
        return "未知";
    }
    switch (Number(status)) {
        case 0 : return "在线";
        case 1 : return "离线";
        default: return "未知";
    }
}
function reload(){
    location.reload();
}
